/*
 * $HeadURL: $
 * $Id: $
 * Copyright (c) 2013 by Wodensoft System, all rights reserved.
 */
package com.wodensoft.core.context;

import java.util.HashMap;
import java.util.Map;

import com.wodensoft.core.exception.ExceptionType;
import com.wodensoft.core.exception.SystemRuntimeException;

/**
 * @author wodenwang
 * 
 */
abstract class BaseContext {

    /**
     * 实际值
     */
    protected Map<String, ?> values = new HashMap<>();

    /**
     * 校验属性是否存在
     * 
     * @param name
     */
    private void checkNull(String name) {
        if (!values.containsKey(name)) {
            throw new SystemRuntimeException(ExceptionType.CONTEXT_EMPTY, "属性[" + name + "]不存在。");
        }
    }

    /**
     * 获取对象
     * 
     * @param name
     * @return
     */
    public Object getObject(String name) {
        checkNull(name);
        Object value = values.get(name);
        return value;
    }

    /**
     * 获取字符串
     * 
     * @param name
     * @returnregex
     */
    public String getString(String name) {
        Object value = getObject(name);
        return value.toString();
    }

    /**
     * 获取字符串数组
     * 
     * @param name
     * @return
     */
    public String[] getStringArray(String name) {
        Object value = getObject(name);
        if (value instanceof String[]) {
            return (String[]) value;
        } else {
            String str = value.toString();
            return str.split(",");
        }
    }

    // FIXME .其他获取属性的方法
}
